﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Collections.ObjectModel;

namespace University.Workshop.Business
{
    public static class TicketManager
    {
        public static Ticket SendTicket(string issueTitle, string description, long orderId, short priority)
        {
            Ticket ticket = null;
            if (orderId != -1)
            {
                ticket = new Ticket { IssueTitle = issueTitle, Description = description, Date = DateTime.Now, OrderId = orderId, Priority = priority, Status = TicketStatus.New };
                return TicketDataAccess.RegisterTicket(ticket);
            }
            else
                throw new BusinessException("You have to select some purchase!");
        }

        public static Collection<PurchasedProduct> GetPurchasesWithoutTicketByUser(string buyerEmail)
        {
            Collection<PurchasedProduct> purchasesInv = new Collection<PurchasedProduct>();
            Collection<PurchasedProduct> purchases = new Collection<PurchasedProduct>();
            User buyer = UserDataAccess.GetUserByEmail(buyerEmail);
            if (buyer != null)
            {
                Collection<Order> orders = OrderDataAccess.GetOrdersByBuyerId(buyer.Id);
                if (orders != null)
                {
                    foreach (Order ord in orders)
                    {
                        Product prod = ProductDataAccess.GetProductById(ord.ProductId);
                        string seller = (UserDataAccess.GetUserById(prod.SellerId)).ContactInformation.Email;
                        if(TicketIsNotAlreadyRegistered(ord.Id))
                            purchasesInv.Add(new PurchasedProduct { Seller = seller, ProductBought = prod, Quantity = ord.Quantity, OrderId = ord.Id });
                    }
                    for (int i = purchasesInv.Count - 1; i >= 0; i--)
                    {
                        purchases.Add(purchasesInv[i]);
                    }
                }
            }
            return purchases;
        }

        private static bool TicketIsNotAlreadyRegistered(long ordId)
        {
            return TicketDataAccess.GetTicketByOrderId(ordId)==null;
        }

        public static bool UserIsTheOwnerOfTicket(string email, long ticketId)
        {
            User user = UserDataAccess.GetUserByEmail(email);
            if (user != null)
            {
                Collection<Ticket> allUserTickets = TicketDataAccess.GetAllTicketsByBuyerId(user.Id);
                if (allUserTickets != null)
                {
                    foreach (var ticket in allUserTickets)
                    {
                        if (ticket.Id == ticketId)
                            return true;
                    }
                }
            }
            return false;
        }

        public static Collection<Ticket> GatherNewestTickets()
        {
            Collection<Ticket> tickets = TicketDataAccess.GatherActiveTickets();
            if (tickets != null)
            {
                Collection<Ticket> ticketsResult = new Collection<Ticket>();
                var orderedTickets = from ticket in tickets
                                     where ticket.Status == TicketStatus.New
                                     orderby ticket.Date descending
                                     select ticket;
                foreach (var item in orderedTickets)
                {
                    ticketsResult.Add(item);
                }
                return ticketsResult;
            }
            return null;
        }

        public static Collection<Ticket> GatherHigherPriorityTickets()
        {
            Collection<Ticket> tickets = TicketDataAccess.GatherActiveTickets();
            if (tickets != null)
            {
                Collection<Ticket> ticketsResult = new Collection<Ticket>();
                var orderedTickets = from ticket in tickets
                                     orderby ticket.Priority descending
                                     select ticket;
                foreach (var item in orderedTickets)
                {
                    ticketsResult.Add(item);
                }
                return ticketsResult;
            }
            return null;
        }

        public static TicketReply RespondTicket(long ticketId, string userEmail, string reply)
        {
            User user = UserDataAccess.GetUserByEmail(userEmail);
            TicketReply ticketReply = new TicketReply
                                          {TicketId = ticketId, UserId = user.Id, Reply = reply, Date = DateTime.Now};
            return TicketDataAccess.RegisterTicketReply(ticketReply);
        }
    }
}
